Imports System
Imports System.Configuration
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Data.SqlClient

<WebService(Namespace:="http://wroxunited.net/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class FixturesService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function Fixtures() As DataSet

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("WroxUnited").ConnectionString)
        Dim adapter As New SqlDataAdapter("SELECT FixtureDate, Opponents, FixtureType, GoalsFor, GoalsAgainst FROM Fixtures ORDER BY FixtureDate", conn)
        Dim ds As New DataSet()

        adapter.Fill(ds, "Fixtures")

        Return ds

    End Function

    <WebMethod()> _
    Public Function FixtureByDate(ByVal FixtureDate As DateTime) As DataSet

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("WroxUnited").ConnectionString)
        Dim adapter As New SqlDataAdapter("usp_FixtureByDate", conn)

        adapter.SelectCommand.CommandType = CommandType.StoredProcedure
        adapter.SelectCommand.Parameters.Add("@FixtureDate", SqlDbType.DateTime).Value = FixtureDate

        Dim ds As New DataSet()

        adapter.Fill(ds, "Fixtures")

        Return ds

    End Function

    <WebMethod()> _
    Public Sub Goal(ByVal FixtureID As Integer, ByVal PlayerID As Integer, ByVal GoalTime As Integer)

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("WroxUnited").ConnectionString)
        Dim cmd As New SqlCommand("usp_Goal", conn)
        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.Add("@FixtureID", SqlDbType.Int).Value = FixtureID
        cmd.Parameters.Add("@PlayerID", SqlDbType.Int).Value = PlayerID
        cmd.Parameters.Add("@GoalTime", SqlDbType.Int).Value = GoalTime

        cmd.ExecuteNonQuery()

    End Sub

    <WebMethod()> _
    Public Sub UpdateScore(ByVal FixtureID As Integer, ByVal GoalFor As Boolean, ByVal PlayerID As Integer, ByVal GoalTime As Integer)


        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("WroxUnited").ConnectionString)
        Dim cmd As New SqlCommand("usp_UpdateScore", conn)
        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.Add("@FixtureID", SqlDbType.Int).Value = FixtureID
        cmd.Parameters.Add("@GoalFor", SqlDbType.Bit).Value = GoalFor

        cmd.ExecuteNonQuery()

        If GoalFor Then
            Goal(FixtureID, PlayerID, GoalTime)
        End If
    End Sub

End Class
